package com.leji.song.myOrder;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import com.dgl.sdk.util.JsonUtil;
import com.dgl.sdk.view.LoadImage;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.leji.song.R;
import com.leji.song.extra.BaseActivity;
import com.leji.song.extra.BaseCoreActivity;
import com.leji.song.httpInterface.BaseAjaxParams;
import com.leji.song.httpInterface.HttpInterface;
import com.leji.song.httpInterface.NomalResult;
import com.leji.song.mainPage.MapActivity;
import com.leji.song.myOrder.model.DS_OrderDetail;
import com.leji.song.myOrder.model.DS_OrderGoods;
import com.leji.song.util.MyAjaxCallBack;
import net.tsz.afinal.FinalHttp;

import java.lang.reflect.Type;
import java.util.List;

/**
 * Created by colin.ding on 2014/7/1.
 */
public class MyOrderDetailActivity extends BaseActivity{

    private TextView shopName;
    private TextView shopTelNum;
    private TextView totalPrice;
    private TextView gotoTime;
    private ImageView callShop;
    private ListView orderListView;
    private View shopInfoView;

    private TextView receiverName;
    private TextView phone;
    private TextView adress;
    private TextView gettoTime;
    private ImageView shopIcon;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setAnimType(RIGHT_ENTER_LEFT_EXT);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.order_detail);

        initView();

        getOrderDetail();
    }


    private void initView() {
        setReturnBtn();

        setActivityTitle("订单详情");

        shopName = (TextView) findViewById(R.id.shopName);
        shopTelNum = (TextView) findViewById(R.id.shopTelNum);
        totalPrice = (TextView) findViewById(R.id.totalPrice);
        gotoTime = (TextView) findViewById(R.id.gotoTime);
        callShop = (ImageView) findViewById(R.id.callShop);
        orderListView = (ListView) findViewById(R.id.orderListView);

        receiverName = (TextView) findViewById(R.id.receiverName);
        phone = (TextView) findViewById(R.id.phone);
        adress = (TextView) findViewById(R.id.adress);
        gettoTime = (TextView) findViewById(R.id.gettoTime);
        shopIcon = (ImageView) findViewById(R.id.shopIcon);

    }


    private void getOrderDetail() {

        BaseAjaxParams params = new BaseAjaxParams();
        params.put(HttpInterface.HIF_GetOrderDetail.params.orderId, getIntent().getStringExtra("orderId"));

        FinalHttp fh = new FinalHttp();
        fh.get(HttpInterface.HIF_GetOrderDetail.address, params, new MyAjaxCallBack<Object>((BaseCoreActivity)this) {
            @Override
            public void onSuccess(Object o) {
                super.onSuccess(o);

                try{
                    Object obj = NomalResult.doResult(o,MyOrderDetailActivity.this);
                    if (obj != null) {

                        Gson gson = new Gson();
                        final DS_OrderDetail orderDetail = JsonUtil.fromJson(gson.toJson(obj), DS_OrderDetail.class);


                        Type listType = new TypeToken<List<DS_OrderGoods>>() {
                        }.getType();
                        List<DS_OrderGoods> goodsList = gson.fromJson(gson.toJson(orderDetail.getGoods()), listType);


                        shopName.setText(orderDetail.getShopsName());
                        shopTelNum.setText("联系电话：" + orderDetail.getShopsPhone());


                        totalPrice.setText("合计：" + orderDetail.getSumprice());


                        gotoTime.setText("点餐时间：" + orderDetail.getTime());

                        receiverName.setText("收货人：" + orderDetail.getUsername());
                        phone.setText("电话：" + orderDetail.getPhone());
                        adress.setText("地址：" + orderDetail.getAddress());
                        gettoTime.setText("送达时间：" + orderDetail.getGettotime());


                        callShop.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {

                                Intent intent=new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + orderDetail.getShopsPhone()));
                                startActivity(intent);
                            }
                        });

                        OrderDetailListAdapter orderDetailListAdapter = new OrderDetailListAdapter(MyOrderDetailActivity.this, goodsList);
                        orderListView.setAdapter(orderDetailListAdapter);

                        LoadImage.display(shopIcon, orderDetail.getImgUrl());
                    }
                }catch (Exception e){
                    e.printStackTrace();
                }


            }
        });
    }


}
